cd "$pathdata/Risk"

use Risk_cleaned, clear



**** Histogram of Cognitive Uncertainty

graph twoway (histogram cognitive_uncertainty if treatment ==0 , frac gap(15) lcolor(black%20)   fcolor(black%20) width(0.049)),  ///
	title("Choice under risk", color(black)) ///
	ytitle("Fraction",) xtitle("Cognitive uncertainty") ///
	graphregion(fcolor(white) lcolor(white)) ///
	ylabel(0(0.04)0.16,glcolor(gs15) ang(h)) xmtick(#20) ///
	xsize(8) ysize(8) ///
	ysc(range(0 0.17) lcolor(none)) xsc(lcolor(none)) ///
	yline(0.16,lstyle(grid)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///



* Figure CU split

preserve
egen cu_cutoff = pctile(cognitive_uncertainty) if treatment==0, p(50)
gen uncertain=(cognitive_uncertainty>cu_cutoff)

egen bucket=group(uncertain lottery_probability)

gen coef_med=.
gen se_med=.
gen coef_mean=.
gen se_mean=.

levelsof bucket

foreach i in `r(levels)'{
	cap qui qreg2 ce if bucket==`i'& treatment==0, cluster(id)
	cap qui replace coef_med=_b[_cons] if bucket==`i'& treatment==0
	cap qui replace se_med=_se[_cons] if bucket==`i'& treatment==0

	cap qui reg ce if bucket==`i'& treatment==0, cluster(id)
	cap qui replace coef_mean=_b[_cons] if bucket==`i'& treatment==0
	cap qui replace se_mean=_se[_cons] if bucket==`i'& treatment==0
}


collapse (p50) ce coef_* se_* cognitive_uncertainty if treatment==0, by(uncertain lottery_probability)

gen upper_med=coef_med+se_med
gen lower_med=coef_med-se_med
gen upper_mean=coef_mean+se_mean
gen lower_mean=coef_mean-se_mean



* Medians
tw (scatter coef_med lottery_probability if uncertain==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter coef_med lottery_probability if uncertain==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper_med lower_med lottery_probability if uncertain==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper_med lower_med lottery_probability  if uncertain==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") title("Choice under risk", color(black)) ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Low cognitive uncertainty") label(2 "High cognitive uncertainty") label(3 "{c 177}1 std. error of median") label(5 "Risk-neutral prediction") r(2))



* Means
tw (scatter coef_mean lottery_probability if uncertain==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter coef_mean lottery_probability if uncertain==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper_mean lower_mean lottery_probability if uncertain==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper_mean lower_mean lottery_probability  if uncertain==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") title("Choice under risk", color(black)) ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Low cognitive uncertainty") label(2 "High cognitive uncertainty") label(3 "{c 177}1 std. error of mean") label(5 "Risk-neutral prediction") r(2))


restore



*** Figure Distance to optimal decision

binscatter abs_dist cognitive_uncertainty_wins if treatment ==0 , ///
		xtitle("Cognitive uncertainty") ytitle("Distance b/w decision and optimal decision") xscale(lcolor(none)) yscale(lcolor(none)) ///
		title("Choice under risk", color(black)) ///
		yline(10, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white)) ///
		ms(+) mcolor(black) lcolor(red%80) ///
		xsize(8) ysize(8) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin))
		


spearman abs_dist cognitive_uncertainty if treatment==0





** Treatment comparison

preserve

egen bucket=group(treatment lottery_probability)
gen se_ce=.

forvalues i=1/22{
	qreg2 ce if bucket==`i', cl(id)
	replace se_ce=_se[_cons] if bucket==`i'
}

collapse (p50) ce cognitive_uncertainty se_ce if round>6, by(treatment lottery_probability)

gen upper=ce+se_ce
gen lower=ce-se_ce

tw (scatter ce lottery_probability if treatment==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter ce lottery_probability if treatment==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper lower lottery_probability if treatment==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper lower lottery_probability  if treatment==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") title("Complex numbers in choice under risk", color(black)) ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Baseline lottery") label(2 "Complex numbers lottery") label(3 "{c 177}1 std. error of median") label(5 "Risk-neutral prediction") r(2))


restore



		
		

*** Plot of CU vs. p

preserve

*keep if treatment == 0
keep if round <= 6

gen se_cu=.

foreach i in 1 5 10 25 35 50 65 75 90 95 99{
	qreg2 cognitive_uncertainty if lottery_probability==`i', cl(id)
	replace se_cu=_se[_cons] if lottery_probability==`i'
}

collapse (p50) cognitive_uncertainty  se_cu, by(lottery_probability)

gen upper=cognitive_uncertainty+se_cu
gen lower=cognitive_uncertainty-se_cu

tw 	(scatter cognitive_uncertainty lottery_probability, msize(medsmall)  ms(o) mcolor(black)) ///
	(rcap upper lower lottery_probability, msiz(large) lcolor(black)),	///
	ytitle("Cognitive Uncertainty") ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(0(0.1).35, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) legend(off)


restore




*** CU quartiles

keep if treatment == 0 

xtile q = cognitive_uncertainty, nquantile(4)

forvalues i=1/4{
	reg ce lottery_probability if q == `i', cl(id)
	estimates store v`i'
}

coefplot (v1, color(black) ciopts(color(black))) (v2, color(black) ciopts(color(black))) (v3, color(black) ciopts(color(black))) (v4, color(black) ciopts(color(black))), drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none)) xscale( lcolor(none))  ylabel(0 (0.2) 0.8, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ysc(r(-0.005,0.04) lcolor(none)) xlabel(0.7 `""CU Q1" "(ave. = 1.7)""' 0.9 `""CU Q2" "(ave. = 14.7)""' 1.1 `""CU Q3" "(ave. = 31.7)""' 1.3 `""CU Q4" "(ave. = 61.9)""') yline(0, lcolor(gs10) lwidth(thin)) grid(glcolor(gs15))  graphregion(color(white) fcolor(white)) ytitle("Regression coefficient and 95% CI") title("Effect of payout probability on certainty equivalents", color(black)) legend(off)





	
** Across Trial Inconsistency 

use $pathdata/Risk/across_trial_inconsistency.dta, clear

gen cognitive_uncertainty_wins=min(cognitive_uncertainty,.6)

binscatter abs_diff_normalized_switch cognitive_uncertainty_wins,  nquant(10) xscale( lcolor(none)) yscale(lstyle(none)) ///
		xtitle("Cognitive uncertainty") ytitle("Absolute difference b/w normalized CE") ///
		title("Choice under risk", color(black)) ///
		graphregion(color(white)) ///
		ms(+) mcolor(black) lcolor(red%80) ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin))
		
		

reg abs_diff_normalized_switch cognitive_uncertainty, cl(id)
reg abs_diff_normalized_switch cognitive_uncertainty_wins, cl(id)
pwcorr abs_diff_normalized_switch cognitive_uncertainty_wins
spearman abs_diff_normalized_switch cognitive_uncertainty_wins
spearman abs_diff_normalized_switch cognitive_uncertainty


*** Risk Default Manipulation ***

use $pathdata/Risk/Risk_Default_Manipulation_cleaned.dta, clear

describe

xtile cu_pct=cognitive_uncertainty, n(2)

sum cognitive_uncertainty,d
gen low_cu=(cognitive_uncertainty<=r(p50))

gen default_coef=.

forvalues i=1/2{
	reg abs_ce low_default if cu_pct==`i', cl(id)
	estimates store risk`i'
}

coefplot (risk1, color(midblue%90) ciopts(color(midblue%90))) (risk2, mcolor(red%80)   ciopts(color(red%80)) lcolor(red%70)), ///
		ytitle("Treatment effect of low default") drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none) r(0, 25)) ylabel(0(5)25, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) bgcolor(white) xla(, tlc(none) labcolor(none)) title("Choice under risk experiments", color(black)) ///
		xscale( lcolor(none)) ysc(lcolor(none)) ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white) fcolor(white)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		legend(order(2 4) label(2 "Below-median CU") label(4 "Above-median CU"))
graph display

